package com.hp.attendance.service;

import com.hp.attendance.dao.overtimeDao;
import com.hp.attendance.entity.Employee;
import com.hp.attendance.entity.Overtime;
import com.hp.attendance.entity.PageInfo;
import com.hp.attendance.entity.dto.OvertimeEmpDto;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OvertimeService {

    private overtimeDao overtimeDao=new overtimeDao();
    private EmployeeService employeeService=new EmployeeService();

    public PageInfo<OvertimeEmpDto> getPage(int pageNo , int pageSize) throws ClassNotFoundException, SQLException, InstantiationException, IOException, IllegalAccessException {
        PageInfo<OvertimeEmpDto> page = new PageInfo<OvertimeEmpDto>();
        List<Overtime> list = overtimeDao.getListOverLimit(pageNo, pageSize);
        List<OvertimeEmpDto> listOE = new ArrayList<>();
        for (Overtime overtime : list) {
            OvertimeEmpDto oed = new OvertimeEmpDto();
            oed.setOvertime(overtime);
            Employee employee = employeeService.getEmployeeByEmpNum(overtime.getEmployee_number());
            oed.setName(employee.getName());
            listOE.add(oed);
        }
        page.setRecords(listOE);
        long total = overtimeDao.getTotal();
        page.setTotal(total);
        page.setCurrent(pageNo);
        int pages=(int) Math.ceil(total/(pageSize*1.0));
        page.setPages(pages);
        return page;
    }

    public void addOvertime(Overtime overtime) throws SQLException, IOException, ClassNotFoundException {
        overtimeDao.addOvertime(overtime);
    }
    public Overtime queryOvertime(int id) throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException, IOException {
        Overtime overtime = overtimeDao.queryOvertime(id);
        return overtime;


    }
    public void updateOvertime(Overtime overtime) throws SQLException, IOException, ClassNotFoundException {
        overtimeDao.updateOvertime(overtime);
    }

    public void delOvertime(int id) throws SQLException, IOException, ClassNotFoundException {
        overtimeDao.delOvertime(id);
    }

}
